package main;

import ipconfig.Ipconfig;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JRootPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.SwingConstants;

import voSetting.LoginVO;
import voSetting.StateSetVO;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import javax.swing.ImageIcon;

import java.awt.Component;

public class Login extends JFrame {
	Point initialClick;
	
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	int width= (int)screenSize.getWidth();
	int height = (int)screenSize.getHeight();
	int taskbarheight = (int) (Toolkit.getDefaultToolkit().getScreenSize().height 
		    - GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().getHeight());
	
	Point point = new Point();
	boolean resizing = false; 
	boolean maximum = false;
	ClientThread clientThread;
	
	Ipconfig ip = new Ipconfig();
	
	private JPanel contentPane;
	private JTextField idField;
	private JPasswordField passwordField;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JButton btnNewButton_2;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JButton button;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Login frame = new Login();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Login() {
		setUndecorated(true);		
		//getRootPane().setWindowDecorationStyle(JRootPane.NONE);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(width/2-200, height/2-115, 400, 230);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLabel_1_1());
		contentPane.add(getLabel_1_2());
		contentPane.add(getIdField());
		contentPane.add(getPasswordField());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_2());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getButton());
		contentPane.add(getLblNewLabel_3());
		contentPane.add(getLblNewLabel_4());
		contentPane.add(getLblNewLabel_2());
		setLocationRelativeTo(null);
		
		addMouseListener(new MouseAdapter() {
	        public void mousePressed(MouseEvent e) {
	            initialClick = e.getPoint();
	            getComponentAt(initialClick);
	        }
	    });

	    addMouseMotionListener(new MouseMotionAdapter() {
	        @Override
	        public void mouseDragged(MouseEvent e) {

	            // get location of Window
	            int thisX = getLocation().x;
	            int thisY = getLocation().y;

	            // Determine how much the mouse moved since the initial click
	            int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
	            int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);

	            // Move window to this position
	            int X = thisX + xMoved;
	            int Y = thisY + yMoved;
	            setLocation(X, Y);
	        }
	    });
		
	}
	private JTextField getIdField() {
		if (idField == null) {
			idField = new JTextField();
			idField.setBounds(131, 112, 161, 21);
			idField.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
			idField.setColumns(10);
		}
		return idField;
	}
	private JPasswordField getPasswordField() {
		if (passwordField == null) {
			passwordField = new JPasswordField();
			passwordField.setBounds(131, 140, 161, 21);
			passwordField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_ENTER){
						btnNewButton.doClick();
					}
				}
			});
			passwordField.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		}
		return passwordField;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("로그인");
			btnNewButton.setForeground(Color.MAGENTA);
			btnNewButton.setIcon(null);
			btnNewButton.setBounds(300, 137, 65, 21);
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.setBorder(null);
			btnNewButton.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() != KeyEvent.VK_ENTER){
						
					}
					else{
						btnNewButton.doClick();
					}
					
				}
			});
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					Socket socket;
					try {
						socket = new Socket(ip.getFIXED_IP(),1111);
						
						clientThread = new ClientThread(socket);
						clientThread.typeChange(Login.this, "login");
						while(clientThread.oos == null){
							clientThread.join(1);
						}
						
						LoginVO lvo = new LoginVO();
						
						lvo.setM_id(idField.getText());
						lvo.setM_pw(passwordField.getText());
						
						StateSetVO ssvo = new StateSetVO();
						ssvo.setVo_type(1);
						ssvo.setVo(lvo);
						
						clientThread.oos.writeObject(ssvo);
						clientThread.oos.flush();
						
					} catch (UnknownHostException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
				}
			});
			btnNewButton.setFont(new Font("맑은 고딕", Font.BOLD, 18));
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("");
			btnNewButton_1.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\image\\btn_join.png"));
			btnNewButton_1.setBounds(53, 185, 81, 25);
			btnNewButton_1.setBackground(Color.WHITE);
			btnNewButton_1.setBorder(null);
			btnNewButton_1.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return btnNewButton_1;
	}
	private JButton getBtnNewButton_2() {
		if (btnNewButton_2 == null) {
			btnNewButton_2 = new JButton("");
			btnNewButton_2.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\image\\btn_idsearch.png"));
			btnNewButton_2.setVerticalTextPosition(SwingConstants.BOTTOM);
			btnNewButton_2.setBounds(190, 185, 81, 25);
			btnNewButton_2.setBackground(Color.WHITE);
			btnNewButton_2.setBorder(null);
			btnNewButton_2.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return btnNewButton_2;
	}
	private JLabel getLabel_1_1() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("I    D");
			lblNewLabel.setBounds(31, 107, 88, 31);
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblNewLabel;
	}
	private JLabel getLabel_1_2() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("PASSWORD");
			lblNewLabel_1.setBounds(31, 137, 88, 31);
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		}
		return lblNewLabel_1;
	}
	
	public void show_exception_message(){
		JOptionPane.showMessageDialog(this, "로그인 정보가 올바르지 않습니다.");
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("New label");
			lblNewLabel_2.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\image\\back_main.png"));
			lblNewLabel_2.setBounds(0, 0, 400, 230);
		}
		return lblNewLabel_2;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("");
			button.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\image\\btn_pwsearch.png"));
			button.setVerticalTextPosition(SwingConstants.BOTTOM);
			button.setFont(new Font("맑은 고딕", Font.BOLD, 12));
			button.setBorder(null);
			button.setBackground(Color.WHITE);
			button.setBounds(272, 185, 100, 25);
		}
		return button;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("");
			lblNewLabel_3.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					setState(JFrame.ICONIFIED);	
				}
			});
			lblNewLabel_3.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\minimum.png"));
			lblNewLabel_3.setBounds(360, 5, 15, 15);
		}
		return lblNewLabel_3;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("");
			lblNewLabel_4.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});

			lblNewLabel_4.setIcon(new ImageIcon("C:\\Program Files\\Mino Corp\\system\\close.png"));
			lblNewLabel_4.setBounds(380, 5, 15, 15);
		}
		return lblNewLabel_4;
	}
}
